1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.Helpers;
24  import com.google.common.collect.testing.features.CollectionSize;
25  import com.google.common.collect.testing.features.MapFeature;
26  
27  /**
28   * Tester for {@code BiMap.put} and {@code BiMap.forcePut}.
29   */
30  @GwtCompatible
31  public class BiMapPutTester<K, V> extends AbstractBiMapTester<K, V> {
32    
33    @SuppressWarnings("unchecked")
34    @MapFeature.Require(SUPPORTS_PUT)
35    @CollectionSize.Require(ZERO)
36    public void testPutWithSameValueFails() {
37      K k0 = samples.e0.getKey();
38      K k1 = samples.e1.getKey();
39      V v0 = samples.e0.getValue();
40      getMap().put(k0, v0);
41      try {
42        getMap().put(k1, v0);
43        fail("Expected IllegalArgumentException");
44      } catch (IllegalArgumentException expected) {
45        // success
46      }
47      // verify that the bimap is unchanged
48      expectAdded(samples.e0);
49    }
50  
51    @SuppressWarnings("unchecked")
52    @MapFeature.Require(SUPPORTS_PUT)
53    @CollectionSize.Require(ZERO)
54    public void testPutPresentKeyDifferentValue() {
55      K k0 = samples.e0.getKey();
56      V v0 = samples.e0.getValue();
57      V v1 = samples.e1.getValue();
58      getMap().put(k0, v0);
59      getMap().put(k0, v1);
60      // verify that the bimap is changed, and that the old inverse mapping 
61      // from v1 -> v0 is deleted
62      expectContents(Helpers.mapEntry(k0, v1));
63    }
64    
65    @SuppressWarnings("unchecked")
66    @MapFeature.Require(SUPPORTS_PUT)
67    @CollectionSize.Require(ZERO)
68    public void putDistinctKeysDistinctValues() {
69      getMap().put(samples.e0.getKey(), samples.e0.getValue());
70      getMap().put(samples.e1.getKey(), samples.e1.getValue());
71      expectAdded(samples.e0, samples.e1);
72    }
73  
74    @SuppressWarnings("unchecked")
75    @MapFeature.Require(SUPPORTS_PUT)
76    @CollectionSize.Require(ZERO)
77    public void testForcePutOverwritesOldValueEntry() {
78      K k0 = samples.e0.getKey();
79      K k1 = samples.e1.getKey();
80      V v0 = samples.e0.getValue();
81      getMap().put(k0, v0);
82      getMap().forcePut(k1, v0);
83      // verify that the bimap is unchanged
84      expectAdded(Helpers.mapEntry(k1, v0));
85    }
86    
87    @SuppressWarnings("unchecked")
88    @MapFeature.Require(SUPPORTS_PUT)
89    @CollectionSize.Require(ZERO)
90    public void testInversePut() {
91      K k0 = samples.e0.getKey();
92      V v0 = samples.e0.getValue();
93      K k1 = samples.e1.getKey();
94      V v1 = samples.e1.getValue();
95      getMap().put(k0, v0);
96      getMap().inverse().put(v1, k1);
97      expectAdded(samples.e0, samples.e1);
98    }
99  }